<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 详情模板ajax
 * @package  	ajax
 * @subpackage  backend
 * @author   	create by allen.qiang
 */
require 'module/base/goodsHandler.class.php';
class ajax_detailSetup {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询详情模板列表
                    $this->searchDetailSetup();
                    break;
			    case 2: //保存详情模板
                    $this->saveDetailSetup();
                    break;
				case 3: //删除商品详情页模板
					$this->deleteDetailSetup();
					break;
				case 4: //查询购物须知列表
					$this->searchShoppingTips();
					break;
				case 5: //保存购物须知
					$this->saveShoppingTips();
                    break;
                case 6: //删除购物须知
					$this->deleteShoppingTips();
					break;
            }
        }
    }
	/**
     * 查询购物须知列表
     */
    private function searchShoppingTips() {

        global $G_PGLOB;
        
        $sys_itemcategory = new entity('sys_itemcategory');
		$sys_itemcategory->itemCategoryName = '购物须知';
		if (!$G_PGLOB->DBCA->fetch($sys_itemcategory)) {
			return null;
		}
        
        $_REQUEST['itemCategoryID'] = $sys_itemcategory->itemCategoryID;
        
        $itemHandler = new itemHandler( $G_PGLOB->DBCA );
		
		$result_data = $itemHandler->searchItem( $_REQUEST );

		$result_array = array(
            'page'  => $arg_array['page'] ,
            'total' => $arg_array['count'] ,
            'rows'  => array()
        );
        
		if ($result_data) {
            foreach($result_data as &$b_l) {
                $result_array['rows'][] = array(
                    'id' 	=> $b_l['itemID'],
                    'cell' 	=> array(
	        			'<a href="javascript: void(0);" onclick=detail.edit(' . $b_l['itemID'] . ');>修改</a>&nbsp;',
	        			$b_l['itemID'],
	        			$b_l['itemName'],
	        			$b_l['extend_1'],
	        			$b_l['useFlg'] ? '<a href="#">启用</a>' : '<a href="#">禁用</a>'
	        		),
                );
            }
        }

        echo json_encode( $result_array );
    }
    
    /**
     * 查询详情模板列表
     */
    private function searchDetailSetup() {

        global $G_PGLOB;
        
        $sortname   = $_REQUEST['sortname'] ;
        $sortorder  = $_REQUEST['sortorder'] ;
        $page	    = $_REQUEST['page'] ;
        $rp		    = $_REQUEST['rp'] ;
        $tid        = isset($_REQUEST['tid']) ? trim($_REQUEST['tid']) : null;
        $title      = isset($_REQUEST['title']) ? trim($_REQUEST['title']) : null;
        
        //查询条件
		$arg_array = array(
			'sortname' 	=> $sortname , 
			'sortorder' => $sortorder , 
			'page' 		=> $page , 
			'rp' 		=> $rp 
		);
		
		//字段查询方式
		$extend_array = array() ;
		if(!empty($tid)) {
			$arg_array['tid'] 	  	= $tid ;
			$extend_array['tid'] 	= 3  ; 
		}
		
		if(!empty($title)) {
			$arg_array['title'] 	= $title ;
			$extend_array['title'] = 1  ; 
		}
		
		$commHandler = new commHandler( $G_PGLOB->DBCA ) ;
		$result_data = $commHandler->search( $arg_array , 'goods_detail_template' , $extend_array ) ;

		$result_array = array(
            'page'  => $arg_array['page'] ,
            'total' => $arg_array['count'] ,
            'rows'  => array()
        );
        
		if ($result_data) {
            foreach($result_data as &$b_l) {
                $result_array['rows'][] = array(
                    'id' 	=> $b_l->tid,
                    'cell' 	=> array(
	        			'<a href="javascript: void(0);" onclick=detail.edit(' . $b_l->tid . ');>修改</a>&nbsp;',
	        			$b_l->tid,
	        			$b_l->title,
	        			$b_l->creator,
						$b_l->createTime
	        		),
                );
            }
        }

        echo json_encode( $result_array );
    }
    
    private function saveDetailSetup () {
    	global $G_PGLOB;
    
    	$_REQUEST['creator'] = $G_PGLOB->currentUserID;
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->saveDetailSetup( $_REQUEST ) ;
    	echo json_encode($result_array);
    }
    
    /**
     * 保存购物须知
     */
    private function saveShoppingTips () {
    	global $G_PGLOB;
    
    	$_REQUEST['creator'] = $G_PGLOB->currentUserID;
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->saveShoppingTips( $_REQUEST ) ;
    	echo json_encode($result_array);
    }
	
	private function deleteDetailSetup () {
		global $G_PGLOB;
		
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->deleteDetailSetup( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
	/**
	 * 删除购物须知
	 *
	 */
	private function deleteShoppingTips () {
		global $G_PGLOB;
		
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->deleteShoppingTips( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	
}
